# cmake in linux

cmake_minimum_required(VERSION 3.8)
if(${CMAKE_VERSION} VERSION_LESS_EQUAL 3.10)
	cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION})
else()
	cmake_policy(VERSION 3.10)
endif()

set(targetName WingsUpdater)
set(targetExtensionName .elf)
set(debugSuffix _d)
set(targetFullName ${targetName}${targetExtensionName})
set(debugMacro ETWS_DEBUG)
if(NOT DEFINED MAJORVER OR MAJORVER STREQUAL "")
set(MAJORVER 0)
endif()
if(NOT DEFINED MINORVER OR MINORVER STREQUAL "")
set(MINORVER 0)
endif()
if(NOT DEFINED PATCHVER OR PATCHVER STREQUAL "")
set(PATCHVER 0)
endif()
message("debug = " ${debug})
if (debug STREQUAL "ON" OR debug STREQUAL "on")
	set(targetFullName ${targetName}${debugSuffix}${targetExtensionName})
	set(extraCxxFlag "-D${debugMacro} -DMAJORVER=${MAJORVER} -DMINORVER=${MINORVER} -DPATCHVER=${PATCHVER} -Wno-comment")
else()
	set(extraCxxFlag "-DMAJORVER=${MAJORVER} -DMINORVER=${MINORVER} -DPATCHVER=${PATCHVER}")
endif()

project(${targetFullName}[C][C++])
message("project name: " ${PROJECT_NAME})
message(STATUS "binary file name: " ${targetFullName})
message("project root dir = " ${PROJECT_SOURCE_DIR})
set(PROJECT_BINARY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/bin)


# 定义easylogging支持线程安全的宏定义
add_definitions(-DELPP_THREAD_SAFE)
# add_definitions(-DELPP_UNICODE)

# cross compile configuration.
# cmake .. -DcrossCompileRootfsPath:STRING=/path/to/rootfs -DcrossCompileGccPath:STRING=/path/to/gcc

if(NOT DEFINED crossCompile OR crossCompile STREQUAL "")
	message(WARNING "host compile.")
	set(cCompilerPath    gcc)
	set(cxxCompilerPath  g++)
	set(CMAKE_SYSTEM_PROCESSOR x86_64) #设置目标处理器架构
else()
	message(WARNING "cross compile.")
	#set(crossCompilePath "/path/to/rootfs")
	set(CMAKE_CROSSCOMPILING TRUE)
	set(CMAKE_SYSTEM_PROCESSOR arm) #设置目标处理器架构
	if(NOT DEFINED crossCompileGccPath OR crossCompileGccPath STREQUAL "")
		set(cCompilerPath    arm-linux-gnueabihf-gcc)
		set(cxxCompilerPath	arm-linux-gnueabihf-g++)
	else()
		set(cCompilerPath    ${crossCompileGccPath}/arm-linux-gnueabihf-gcc)
		set(cxxCompilerPath  ${crossCompileGccPath}/arm-linux-gnueabihf-g++)
	endif()
endif()

message(STATUS "cCompilerPath  "  ${cCompilerPath})
message(STATUS "cxxCompilerPath  "  ${cxxCompilerPath})


set(CMAKE_C_COMPILER    ${cCompilerPath})
set(CMAKE_CXX_COMPILER  ${cxxCompilerPath})
set(CMAKE_SYSTEM_NAME Linux)

set(CMAKE_SYSROOT ${crossCompileRootfsPath})
set(CMAKE_FIND_ROOT_PATH  ${crossCompileRootfsPath} ${SYSROOT_PATH})
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM  NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY  ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE  ONLY)

set(CMAKE_SKIP_BUILD_RPATH FALSE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
set(CMAKE_INSTALL_RPATH	
	"$ORIGIN" 
	"$ORIGIN/lib" 
	"$ORIGIN/../lib" 
	"$ORIGIN/../../lib" 
	
)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

message(STATUS "cross compile rootfs root path: " ${crossCompileRootfsPath})
set(CMAKE_CXX_STANDARD 17)
set(prefix ${CMAKE_CURRENT_SOURCE_DIR})
message(prefix = ${prefix})

###############################################################################################################################################################################
set(includePath 
	${prefix}/inc 
	${prefix}/inc/Service 
	${prefix}/inc/DataTransmission 
	${prefix}/inc/DataTransmission/Proto 
	${prefix}/inc/PackageResolver 
)

set(srcPath 
	${prefix}/src 
	${prefix}/src/Service 
	${prefix}/src/Misc 
	${prefix}/src/DataTransmission 
	${prefix}/src/DataTransmission/Proto 
	${prefix}/src/PackageResolver 
	${prefix}/src/Wings 
	${prefix}/src/IniConf 
)

set(libSrcPath 
	# ${prefix}/src/Proto/FastDDS 

)

set(libs 
	${prefix}/lib/${CMAKE_SYSTEM_PROCESSOR}/libmyLog.so 
	pthread 
	stdc++fs 
) 

# link_directories( 
# 	${prefix}/lib 

# )

###############################################################################################################################################################################

set(projectBuildPath ${prefix}/build)
set(debugFileGeneratePath ${projectBuildPath}/debug)
set(releaseFileGeneratePath ${projectBuildPath}/release)

macro(checkDir dir)
	if(EXISTS ${dir})
		if(IS_DIRECTORY ${dir})
			message("directory \"${dir}\" already exists!")
		else()
			file(RENAME ${dir} ${dir}.old)
			file(MAKE_DIRECTORY ${dir})
			message("rename file ${dir} and create directory: ${dir}")
		endif()
	else()
		file(MAKE_DIRECTORY ${dir})
		message("create new directory: ${dir}")
	endif()
endmacro()

checkDir(${projectBuildPath})

message("project bin dir = " ${PROJECT_BINARY_DIR})
message("current file name: " ${CMAKE_CURRENT_LIST_FILE})
message("current line number: " ${CMAKE_CURRENT_LIST_LINE})

macro(addSuffix srcList suffix)
	foreach(tmp ${${srcList}})
		if(DEFINED tmpList)
			list(APPEND tmpList ${tmp}${suffix})
		else()
			set(tmpList ${tmp}${suffix})
		endif()
	endforeach()
	set(${srcList} ${tmpList})
	unset(tmpList)
	message(${${srcList}})
endmacro()

message("*****************************************************************************************")
if (debug STREQUAL "ON" OR debug STREQUAL "on")
	set(buildVersion debug) 
	# addSuffix(libs _d) 
	# set(librarySuffix _d) 
	checkDir(${debugFileGeneratePath})
	set(CMAKE_CXX_FLAGS "-g -O0 -std=c++${CMAKE_CXX_STANDARD} -v -Wl,--verbose -Wall -fPIC ${CMAKE_CXX_FLAGS} ${extraCxxFlag}")
else()
	set(buildVersion release)
	checkDir(${releaseFileGeneratePath})
	set(CMAKE_CXX_FLAGS "-std=c++${CMAKE_CXX_STANDARD} -Wall -O3 -fPIC ${CMAKE_CXX_FLAGS} ${extraCxxFlag}")
endif()
message(WARNING "CXX Flag: " ${CMAKE_CXX_FLAGS})
message("build version: " ${buildVersion})
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/${buildVersion})
message("executable path: " ${EXECUTABLE_OUTPUT_PATH})
message("*****************************************************************************************")

# src sub dir.
foreach(subDir ${srcPath})
	message("child source directory: " ${subDir})
	aux_source_directory(${subDir} srcFiles) 	#top cmake file's variable will be delivered to other sub cmake files,so we destroy it at the end int this CMake file.
endforeach(subDir ${srcPath})

foreach(item ${srcFiles})
	message("file ${item}")
endforeach(item ${srcFiles})

# src header sub dir.
include_directories(${includePath})

# lib src sub dir.
foreach(subDir ${libSrcPath})
	message("child library source directory: " ${subDir})
	add_subdirectory(${subDir})
endforeach(subDir ${libSrcPath})

add_executable(${targetFullName} ${srcFiles})
target_link_libraries(${targetFullName} ${libs})

file(COPY conf/Conf.ini DESTINATION ${EXECUTABLE_OUTPUT_PATH})
file(COPY conf/Description.ini DESTINATION ${EXECUTABLE_OUTPUT_PATH})
file(COPY conf/mylog.conf DESTINATION ${EXECUTABLE_OUTPUT_PATH})

#other internal system information variables
message("CMAKE_MAJOR_VERSION = " ${CMAKE_MAJOR_VERSION})
message("CMAKE_MINOR_VERSION = " ${CMAKE_MINOR_VERSION})
message("CMAKE_PATCH_VERSION = " ${CMAKE_PATCH_VERSION})
message("CMAKE_SYSTEM = " ${CMAKE_SYSTEM})
message("CMAKE_SYSTEM_NAME = " ${CMAKE_SYSTEM_NAME})
message("CMAKE_SYSTEM_VERSION = " ${CMAKE_SYSTEM_VERSION})
message("CMAKE_SYSTEM_PROCESSOR = " ${CMAKE_SYSTEM_PROCESSOR})
message("UNIX = " ${UNIX})

unset(srcFiles)
